﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pub;
using System.Web.Configuration;

namespace LZQXT.Test.Forms.Encrypt
{
    public partial class FEnMain : Form
    {
        public FEnMain()
        {
            InitializeComponent();
        }

        private void FEnMain_Load(object sender, EventArgs e)
        {
            tbox2.Dock = DockStyle.Fill;
            panelOpt.Visible = false;

            string[] sType = new string[] { "TEA加密", "TEA解密", "MD5加密", "SHA1加密", "AES加密", "AES解密", "MD5解密" };

            for (int i = 0; i < sType.Length; i++)
            {
                cboxType.Items.Add(sType[i]);
            }
            cboxType.SelectedIndex = 0;
        }

        private void FEnMain_Resize(object sender, EventArgs e)
        {
            tbox1.Width = this.Width / 2 - 3;
        }

        private void cboxType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboxType.Text.IndexOf("TEA") != -1 || cboxType.Text.IndexOf("AES") != -1)
            {
                //panelOpt.Visible = true;
                labKey.Visible = true;
                tboxKey.Visible = true;
            }
            else
            {
                //panelOpt.Visible = false;
                labKey.Visible = false;
                tboxKey.Visible = false;
            }
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            CDebug.EnterMethod("执行");
            try
            {
                tbox2.Text = "";
                switch (cboxType.SelectedIndex)
                {
                    case 0:     //tea加密
                        tbox2.Text = CTEA.TEAEncryptStr(tbox1.Text, tboxKey.Text);
                        break;
                    case 1:     //tea解密
                        tbox2.Text = CTEA.TEADecryptStr(tbox1.Text, tboxKey.Text);
                        break;
                    case 2:     //MD5加密
                        //tbox2.Text = System.Web.Security.FormsAuthentication
                        //    .HashPasswordForStoringInConfigFile(tbox1.Text, "MD5");
                        tbox2.Text = "16位：\n" + CMD5.GetMd5Str16(tbox1.Text) + "\n" 
                            + "32位：\n" + CMD5.GetMd5Str(tbox1.Text);
                        break;
                    case 3:     //SHA1加密
                        //tbox2.Text = System.Web.Security.FormsAuthentication
                        //    .HashPasswordForStoringInConfigFile(tbox1.Text, "SHA1");
                        tbox2.Text =SHA1.GetSHA1Str(tbox1.Text);
                        break;
                    case 4:     //AES加密
                        tbox2.Text = CAES.AESEncryptStr(tbox1.Text, tboxKey.Text);
                        break;
                    case 5:     //AES解密
                        tbox2.Text = CAES.AESDecryptStr(tbox1.Text, tboxKey.Text);
                        break;
                    case 6:     //MD5解密

                        break;
                }
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }
            CDebug.ExitMethod("执行");
        }
    }

    public static class JTBOX
    {
        /// <summary>
        /// 注册插件
        /// </summary>
        /// <param name="node1"></param>
        /// <param name="list1"></param>
        /// <returns></returns>
        public static bool RegPlug(List<Form> list1, ImageList imgLst)
        {
            try
            {
                FEnMain f1 = new FEnMain();
                list1.Add(f1);

                imgLst.Images[list1.Count - 1] = f1.imageList2.Images[0];
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                return false;
            }
            return true;
        }
    }
}
